import { Args, Context, Mutation, Query, Resolver } from '@nestjs/graphql'
import { UserService } from './user.service';
import { UserInput } from './dto/user-input.type';
// import { Query } from '@nestjs/common';
import { UserType } from './dto/user.type';
import { GqlAuthGuard } from '@/common/guards/auth.guards';
import { UseGuards } from '@nestjs/common';
import { Result } from '@/common/dto/result.type';
import { SUCCESS, UPDATE_ERROR } from '@/common/constants/code';

@Resolver(()=>UserType)
@UseGuards(GqlAuthGuard) //启用守卫
export class UserResolver {
    constructor(private readonly userService: UserService) { }

    @Mutation(() => Boolean, { description: '新增用户' })
    async create(@Args('params') params: UserInput): Promise<boolean> {
        return await this.userService.create(params)
    }
    @Query(() => UserType, { description: '使用ID查询用户' })
    async find(@Args('id') id: string): Promise<UserType> {
        return await this.userService.findById(id)
    }
    @Query(() => UserType, { description: '使用ID查询用户' })
    async getUserInfo(@Context() cxt: any): Promise<UserType> {
        // console.log('getUserInfo',cxt.req)
        const id = cxt.req?.user?.id || '';
        return await this.userService.findById(id)
    }
    @Mutation(() => Result, { description: '使用ID查询用户' })
    async updateUserInfo(@Args('id') id:string, @Args('params') params:UserInput): Promise<Result> {
        // console.log('getUserInfo',cxt.req)
        const res = await this.userService.update(id, params)
        if(res){
            return {
                code:SUCCESS,
                message:'更新成功'
            }
        }
        return {
                code:UPDATE_ERROR,
                message:'更新失败'
            }
    }
    @Mutation(() => Boolean, { description: '更新用户' })
    async update(
        @Args('id') id: string,
        @Args('params') params: UserInput
    ): Promise<boolean> {
        return await this.userService.update(id, params)
    }
    @Mutation(() => Boolean, { description: '删除用户' })
    async del(
        @Args('id') id: string
    ): Promise<boolean> {
        return await this.userService.del(id)
    }
}